<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_product extends MY_Model { 
	protected $offset = 0;
	protected $limit = 30;
	function __construct() {
		parent::__construct();
	}
	function get_all($cate_id = '')
	{
		$this->db->select('p.*,c.title category_title,c.slug category_slug')
			->from('product p')
			->join('category c', 'p.category_id=c.id')
			->limit($this->limit, $this->offset);
		if(!empty($cate_id)){
			$this->db->where('c.id',$cate_id);
		}
		return $this->db->get()->result_array();
	}
	function get($id='')
	{
		return $this->db->get_where('product', array('id' => $id))->row_array();
	}

	function get_next($id){
		$this->db->select('p.*')
			->from('product p')
			->where('p.id >',$id)
			->order_by('p.id','asc')
			->limit(1);
		return $this->db->get()->row_array();
	}

	function get_prev($id){
		$this->db->select('p.*')
			->from('product p')
			->where('p.id <',$id)
			->order_by('p.id','desc')
			->limit(1);
		return $this->db->get()->row_array();
	}
	function get_related_product($id='')
	{
		if(empty($id)){
			return array();
		}
		return $this->db->from('product p')
				->join('product r', 'p.category_id=r.category_id')
				->where(array(
							'r.status' => 1, 
							'r.id !=' => $id,
							'p.id' => $id))
				->limit(3, 0)
				->get()
				->result_array();
	}

	function delete($id)
	{
		if(empty($id)){
			return true;
		}
		$this->db->delete('product', array('id' => $id)); 
		return true;
	}

	function delete_home_highlight($id)
	{
		if(empty($id)){
			return true;
		}
		$this->db->delete('home_highlight', array('id' => $id)); 
		return true;
	}

	function delete_product_highlight($id)
	{
		if(empty($id)){
			return true;
		}
		$this->db->delete('product_highlight', array('id' => $id)); 
		return true;
	}
	
	function save($data='')
	{
		if(empty($data)){
			return false;
		}
		
		if (!empty($data['id'])) {
			$id = $data['id'];
			unset($data['id']);
			$this->db->set('update_date', 'now()', false);
			$this->db->update('product', $data, array('id' => $id));
			return $id;
		}else{
			$this->db->insert('product', $data);
			return $this->db->insert_id();
		}
	}
	
	function get_product_highlight($id = '')
	{
		if(!empty($id)){
			return $this->db->get_where('product_highlight', array('id'=>$id))->row_array();
		}
		return $this->db->get('product_highlight')->result_array();
	}
	
	function save_product_highlight($data='')
	{
		if(empty($data)){
			return false;
		}
		
		if (!empty($data['id'])) {
			$id = $data['id'];
			unset($data['id']);
			$this->db->update('product_highlight', $data, array('id' => $id));
			return $id;
		}else{
			$this->db->insert('product_highlight', $data);
			return $this->db->insert_id();
		}
	}
	
	function get_home_highlight($id = '')
	{
		if(!empty($id)){
			return $this->db->get_where('home_highlight', array('id'=>$id))->row_array();
		}
		return $this->db->get('home_highlight')->result_array();
	}
	
	function save_home_highlight($data='')
	{
		if(empty($data)){
			return false;
		}
		
		if (!empty($data['id'])) {
			$id = $data['id'];
			unset($data['id']);
			$this->db->update('home_highlight', $data, array('id' => $id));
			return $id;
		}else{
			$this->db->insert('home_highlight', $data);
			return $this->db->insert_id();
		}
	}
}